<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<List<SelectListItem>>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript" src="../../Scripts/jquery-1.2.6.min.js"></script>
</head>
<body>
     <div>
     <%using (Html.BeginForm("CreateDatabase", "Setup", FormMethod.Post, new { id = "form1" }))
       { %>
        <%=Html.AntiForgeryToken()%>
    <table>
        <tr>
            <td>
                Connection string: (you need to create the database and we'll create the tables)
            </td>
            <td>
                <%=Html.TextArea("connectionString") %>
            </td>
        </tr>
        <tr>
            <td>
            
            </td>
            <td>
                 <%=Html.DropDownList("dbType", ViewData.Model)%>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="hidden" name="deleteExisting" value="false" />
                <input type="button" id="btnSubmit" value="Create Database" onclick="javascript:createDataBase()" />
            </td>
        </tr>
        <tr>
        <td colspan="2">
            <div id="divMessages"></div>
        </td>
            </tr>
    </table>
    <%} %>
    </div>
    <script type="text/javascript">
        var deleteExisting = false;
        function createDataBase() {
            if (document.getElementById('connectionString').value == '') {
                alert('Please enter the connection string');
                return;
            }
            var form = document.getElementById('form1');
            document.getElementById('btnSubmit').disabled = true;
            document.getElementById('divMessages').innerHTML = 'Please wait...';

            $.ajax({
                type: form.method,
                url: form.action,
                data: $(form).serialize(),
                dataType: 'json',
                complete: function() {
                    document.getElementById('btnSubmit').disabled = false;
                    document.getElementById('divMessages').innerHTML = '';
                    document.getElementById('deleteExisting').value = 'false';//reset just in case someone decides to try another connection string
                },
                success: function(j) {
                    if (j.error) {
                        alert(j.message);
                    } else if (j.confirm) {
                        deleteExisting = confirm('It seems that the database tables already exist, would like you delete the old one and create new?');
                        document.getElementById('deleteExisting').value = deleteExisting.toString();
                        if (deleteExisting) {
                            createDataBase();
                        }
                    } else if (j.success) {
                        alert('The tables was successfully created, please update the connection string in the SiteConfig.xml file');
                    }
                }, error: function(err) {
                    alert('An error occurred');
                }
            });
        }
    </script>
</body>
</html>
